<?php
namespace data\site;
use \data\BaseData;
/**
 * 站点数据管理
 */
class SiteData extends BaseData{
    
    
    /**
     * 读取一个站点信息
     */
    public function find($param){
        $fields = ['siteid','name'=>'like','domain'];
        $where = $this->parseWhere($fields,$param);
        if( !$where )  dataE('查询条件不能为空');
        $info  = M('site')->where($where)->find();
        return $info;
    }
    
    /**
     * 添加一个站点信息
     */
    public function add($data){        
        $check = [                    
            //'opentype'      => ['reg'=>[1,2,3,4]],
            //'adduserid'     => ['reg'=>'int'], 
            'msiteid'       => ['check'=>'must','站点主主信息不能为空','reg'=>'int'],
            'domaintype'    => ['reg'=>[1,2,3]],
            'domain'        => '',// ['check'=>'must','msg'=>'域名不能为空'],
            'status'        => ['def'=>1,'reg'=>[1,2]],
            'logo'          => '',
            'name'          => ['check'=>'must','msg'=> '请填写站点名称'],
            'contactname'   => ['check'=>'must','msg'=> '请填写联系人姓名'],
            'contacttel'    => ['check'=>'must','msg'=> '请填写联系方式'],
            'provinceid'    => ['reg'=> 'int'],
            'cityid'        => ['reg'=> 'int'],
            'areaid'        => ['reg'=> 'int'],
            'streetid'      => ['reg'=> 'int'],
            'address'       => '',
            'addressdetail' => '',
            'addtime'       => ['val'=> SYS_TIME],
            'edittime'      => ['val'=> 0],
        ]; 
       // if( $data['domaintype'] < 3 ) $check['domain'] = ['check'=>'must','msg'=>'域名不能为空'];
        $save = $this->verifyData($check,$data);
        return M('site')->insertGetId($save);
    }
    
    
    /**
     * 修改一个站点信息
     */
    public function edit($siteid, $data){
        if( !$siteid ) dataE('更新参数不能为空');
        $check = [                    
            'domain'        => '',
            'status'        => ['reg'=>[1,2]],
            'logo'          => '',
            'name'          => ['reg'=>'must','msg'=>'站点名称不能填空'],
            'contactname'   => ['reg'=>'must','msg'=>'联系人不能填空'],
            'contacttel'    => ['reg'=>'must','msg'=>'联系方式不能填空'],
            'provinceid'    => ['reg'=> 'int'],
            'cityid'        => ['reg'=> 'int'],
            'areaid'        => ['reg'=> 'int'],
            'streetid'      => ['reg'=> 'int'],
            'address'       => '',
            'addressdetail' => '',
            'edittime'      => ['val'=> SYS_TIME],
        ]; 
          
        $save = $this->verifyData($check,$data);
        if( !$save ) return false;
        $where = ['siteid'=>$siteid];
        $result = M('site')->where($where)->update($save);
        if($result === false ) dataE('修改站点信息失败');
        return $result;
    }
    
}